//package com.zxcl.service.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.zxcl.constant.BusinessEnum;
//import com.zxcl.domain.*;
//import com.zxcl.dto.OrderConfirmDTO;
//import com.zxcl.dto.PageDTO;
//import com.zxcl.ex.BusinessException;
//import com.zxcl.feign.OrderCartFeign;
//import com.zxcl.feign.OrderMemberFeign;
//import com.zxcl.feign.OrderProdFeign;
//import com.zxcl.feign.OrderStoreFeign;
//import com.zxcl.model.ProdConfirmModel;
//import com.zxcl.model.Result;
//import com.zxcl.model.ShopOrder;
//import com.zxcl.model.SkuBuyModel;
//import com.zxcl.utils.AuthUtil;
//import com.zxcl.vo.OrderConfirmVO;
//import com.zxcl.vo.OrderStatusVO;
//import com.zxcl.vo.OrderVO;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.stream.Collectors;
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.zxcl.mapper.OrderMapper;
//import com.zxcl.service.OrderService;
//import org.springframework.util.CollectionUtils;
//
//
////@Service
//public class OrderServiceImplBak extends ServiceImpl<OrderMapper, Order> implements OrderService{
//
//

//
//    @Autowired
//    private OrderMemberFeign orderMemberFeign;
//
//    @Autowired
//    private OrderProdFeign orderProdFeign;
//
//    @Autowired
//    private OrderStoreFeign orderStoreFeign;
//
//    @Autowired
//    private OrderCartFeign orderCartFeign;
//
//    /**
//     * 查询订单状态数量
//     *
//     * @return
//     */
//
//    @Override
//    public OrderStatusVO orderStatusCount() {
//        OrderStatusVO orderStatusVO = new OrderStatusVO();
//        String openId = AuthUtil.getOpenId();
//        List<Order> orderList = getBaseMapper().selectList(new LambdaQueryWrapper<Order>()
//                .select(Order::getStatus)
//                .eq(Order::getOpenId, openId)
//                .eq(Order::getDeleteStatus, 0)
//        );
//        if (!CollectionUtils.isEmpty(orderList)) {
//            long unPay = orderList.stream().filter(order -> order.getStatus().equals(1)).count();
//            long payed = orderList.stream().filter(order -> order.getStatus().equals(2)).count();
//            long consignment = orderList.stream().filter(order -> order.getStatus().equals(3)).count();
//            orderStatusVO.setConsignment(consignment);
//            orderStatusVO.setPayed(payed);
//            orderStatusVO.setUnPay(unPay);
//        }
//        return orderStatusVO;
//    }
//
//
////        Integer AllStatus=0;
////        Integer noDeleteStatus=0;
////        String openId = AuthUtil.getOpenId();
////        Page<OrderDetailVO> page = new Page<>();
////        Page<Order> orderPage = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
////        Page<Order> orderPage1 = getBaseMapper().selectPage(orderPage, new LambdaQueryWrapper<Order>()
////                .eq(Order::getOpenId, openId)
////                .eq(!AllStatus.equals(status), Order::getStatus, status)
////                .eq(Order::getDeleteStatus, noDeleteStatus));
////        page.setTotal(orderPage1.getTotal());
////        page.setSize(orderPage1.getSize());
////        page.setCurrent(orderPage1.getCurrent());
////        List<Order> records = orderPage1.getRecords();
////        //创建一个接受商品详情表的集合
////        List<OrderDetailVO> orderItemsAll = new ArrayList<>();
////        for (Order record : records) {
////            OrderDetailVO orderDetailVO = new OrderDetailVO();
////            BeanUtils.copyProperties(record,orderDetailVO);
////            List<OrderItem> orderItems = orderItemService.getBaseMapper().selectList(new LambdaQueryWrapper<OrderItem>()
////                    .eq(OrderItem::getOrderNumber, record.getOrderNumber()));
////            orderDetailVO.setOrderItemDtos(orderItems);
////            orderItemsAll.add(orderDetailVO);
////        }
////        //放入分页数据中
////        page.setRecords(orderItemsAll);
////        return page;
//
//    /**
//     * 分页查询订单列表
//     * @param pageDTO
//     * @param status
//     * @return
//     */
//    @Override
//    public Page<OrderVO> loadOrderPage(PageDTO pageDTO, Integer status) {
//        Page<OrderVO> page = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
//
//        String openId = AuthUtil.getOpenId();
//
//        if (status == 0){
//            Page<OrderVO> orderPage = orderVOMapper.selectPage(page, new LambdaQueryWrapper<OrderVO>()
//                    .eq(OrderVO::getOpenId, openId)
//                    .eq(OrderVO::getDeleteStatus, 0)
//            );
//
//
//            return orderPage;
//
//        }
//        Page<OrderVO> orderPage = orderVOMapper.selectPage(page, new LambdaQueryWrapper<OrderVO>()
//                .eq(OrderVO::getOpenId, openId)
//                .eq(OrderVO::getDeleteStatus, status)
//        );
//        return orderPage;
//    }
//
//    /**
//     * 订单确认
//     * 单品确认
//     *  shopDetail  OrderItem  sku  member_addr
//     * @param orderConfirmDTO
//     * @return
//     */
//    @Override  //OrderConfirmDTO 订单确认对象
//    public OrderConfirmVO orderConfirm(OrderConfirmDTO orderConfirmDTO) {
//        OrderConfirmVO orderConfirmVO = new OrderConfirmVO();
//
//        //查询当前用户默认收获地址
//        Result<MemberAddr> defaultAddrResult = orderMemberFeign.getDefaultAddr(AuthUtil.getOpenId());
//        //没有考虑远程调用失败的问题，因为这里如果失败了，我也可以点进去设置它的收货地址  熔断自行处理
//        MemberAddr memberAddr = defaultAddrResult.getData();
//        orderConfirmVO.setMemberAddr(memberAddr);
//
//        //创建一个计算费用的对象
//        List<SkuBuyModel> skuBuyModels = new ArrayList<>();
//
//        //判断是单品还是购物车入口
//        List<Long> basketIds = orderConfirmDTO.getBasketIds();
//        if (CollectionUtils.isEmpty(basketIds)){
//            //单品调用的方法
//            prod2Confirm(orderConfirmVO,orderConfirmDTO.getProdConfirmModel(),skuBuyModels); //skuBuyModels 商品购买模型
//        }
//        else {
//            //购物车调用的方法
//            cart2Confirm(orderConfirmVO, orderConfirmDTO.getBasketIds(), skuBuyModels);
//
//        }
//        //计算费用
//
//        Integer totalCount = 0;
//        BigDecimal totalMoney = BigDecimal.ZERO;
//        BigDecimal actualTotal = BigDecimal.ZERO;
//        BigDecimal transFee =BigDecimal.ZERO;
//        BigDecimal shopReduce =BigDecimal.ZERO;
//
//        for (SkuBuyModel skuBuyModel : skuBuyModels) {
//            totalCount+=skuBuyModel.getCount();
//            totalMoney=totalMoney.add(skuBuyModel.getPrice()).multiply(new BigDecimal(totalCount));
//        }
//        if (totalMoney.compareTo(new BigDecimal(99))<0){
//            transFee = new BigDecimal(6);
//        }
//        //
//        actualTotal = totalMoney.subtract(shopReduce).add(transFee);
//
//        orderConfirmVO.setActualTotal(actualTotal);
//        orderConfirmVO.setShopReduce(shopReduce);
//        orderConfirmVO.setTotalCount(totalCount);
//        orderConfirmVO.setTransfee(transFee);
//        orderConfirmVO.setTotalMoney(totalMoney);
//        return orderConfirmVO;
//    }
//
//
//    /**
//     * 购物车确认
//     * @param orderConfirmVO
//     * @param basketIds
//     * @param skuBuyModels
//     */
//    private void cart2Confirm(OrderConfirmVO orderConfirmVO, List<Long> basketIds, List<SkuBuyModel> skuBuyModels) {
//
//        //根据购物车的ids拿购物车数据
//        Result<List<Basket>> basketsResult = this.orderCartFeign.getBasketsByIds(basketIds);
//        if (basketsResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
//            throw new BusinessException(basketsResult.getMsg());
//        }
//        //取出购物车数据 既是basket每一行
//        List<Basket> basketList = basketsResult.getData();
//        //取出购物车里面的店铺IDS  每一行的list集合编程list里面装的id
//        List<Long> shopIds = basketList.stream().map(Basket::getShopId).collect(Collectors.toList());
//        //取出购物车里面的skuIds
//        List<Long> skuIds = basketList.stream().map(Basket::getSkuId).collect(Collectors.toList());
//
//        //根据shopIds拿店铺(多个)
//        Result<List<ShopDetail>> shopDetailResult = this.orderStoreFeign.getShopDetailsByIds(shopIds);
//        //处理调用异常的问题(自行处理熔断)
//        if (shopDetailResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
//            throw new BusinessException(shopDetailResult.getMsg());
//        }
//
//        //取出店铺数据
//        List<ShopDetail> shopDetails = shopDetailResult.getData();
//
//        //拿sku  既是取出商品详情数据
//        Result<List<Sku>> skusResult = this.orderProdFeign.getSkusByIds(skuIds);
//        //处理调用异常的问题(自行处理熔断)
//        if (skusResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
//            throw new BusinessException(skusResult.getMsg());
//        }
//        //取出List<Sku>
//        List<Sku> skuList = skusResult.getData();
//
//        //把shopDetails和skuList转成Map<Id,Obj>  这样可以根据主键id查找
//
//        Map<Long, ShopDetail> shopDetailMap = shopDetails.stream().collect(Collectors.toMap(ShopDetail::getShopId, shopDetail -> shopDetail));
//        Map<Long, Sku> skuMap = skuList.stream().collect(Collectors.toMap(Sku::getSkuId, sku -> sku));
//        //组装数据
//        //创建店铺的集合
//        List<ShopOrder> shopOrders = new ArrayList<>();
//        /** 购物车数据
//         * skuId  shopId   count
//         * 1        1       2
//         * 2        1       4
//         */
//
//        basketList.forEach(basket -> {
//            Long skuId = basket.getSkuId();
//            Long shopId = basket.getShopId();
//            Integer prodCount = basket.getProdCount();
//            //根据shopId取出shop的数据
//            ShopDetail shopDetail = shopDetailMap.get(shopId);
//            //shopOrders 一开始是 new ArrayList<>(); 因此.filter里面也是空至于等于那也是空的呀
//            List<ShopOrder> shopOrderList = shopOrders.stream()
//                    .filter(shopOrder -> shopOrder.getShopId()
//                            .equals(shopId)).collect(Collectors.toList()); //根据指定的条件
//            if(CollectionUtils.isEmpty(shopOrderList)){
//                //因为上面的缘故可以想到,shopOrderList一开始是空的, 因此他这个if里写的逻辑就是new 一个shopOrder组装好数据往shoporders中放
//                ShopOrder shopOrder = new ShopOrder();
//                shopOrder.setShopId(shopId);
//                shopOrder.setShopLogo(shopDetail.getShopLogo());
//                shopOrder.setShopName(shopDetail.getShopName());
//                shopOrder.setShopLink(shopDetail.getShopAddress());
//                //创建店铺的商品集合
//                List<OrderItem> shopOrderItems = new ArrayList<>();
//                //创建OrderItem
//                OrderItem orderItem = new OrderItem();
//                //从skuMap里面取出sku
//                Sku sku = skuMap.get(skuId);
//                BeanUtils.copyProperties(sku, orderItem);
//                //设置数量
//                orderItem.setProdCount(prodCount);
//                //把orderItem添加到shopOrderItems
//                shopOrderItems.add(orderItem);
//                //把shopOrderItems交给shopOrder
//                shopOrder.setShopOrderItems(shopOrderItems);
//                //把shopOrder添加到shopOrders
//                shopOrders.add(shopOrder);
//                //组装算总金额数组
//                skuBuyModels.add(new SkuBuyModel(skuId, sku.getPrice(), prodCount));
//            }else{
//                //可是由于这在循环中,所以第二次shopOrderList肯定不为空,因为shopOrders在第一次if中进行了add操作
//                //那这一行就等于是在说取出第一个shopOrder, shopList里面装的是什么?
//                ShopOrder shopOrder = shopOrderList.get(0);
//                //创建店铺的商品集合
//                List<OrderItem> shopOrderItems =shopOrder.getShopOrderItems();
//                //创建OrderItem
//                OrderItem orderItem = new OrderItem();
//                //从skuMap里面取出sku
//                Sku sku = skuMap.get(skuId);
//                BeanUtils.copyProperties(sku, orderItem);
//                //设置数量
//                orderItem.setProdCount(prodCount);
//                //把orderItem添加到shopOrderItems
//                shopOrderItems.add(orderItem);
//                //组装算总金额数组
//                skuBuyModels.add(new SkuBuyModel(skuId, sku.getPrice(), prodCount));
//            }
//        });
//        //放到orderConfirmVO
//        orderConfirmVO.setShopOrders(shopOrders);
//
//    }
//
//    /**
//     * 单品确认
//     * 店铺信息和商品信息
//     * @param orderConfirmVO
//     * @param prodConfirmModel
//     * @param skuBuyModels
//     */
//    private void prod2Confirm(OrderConfirmVO orderConfirmVO, ProdConfirmModel prodConfirmModel, List<SkuBuyModel> skuBuyModels) {
//        //先拿到店铺id和商品id，当然这肯定是前端传过来的
//        Long shopId = prodConfirmModel.getShopId();
//        Long skuId = prodConfirmModel.getSkuId();
//        //商品数量
//        Integer prodCount = prodConfirmModel.getProdCount();
//
//        //拿店铺
//        Result<List<ShopDetail>> shopResult = orderStoreFeign.getShopDetailsByIds(Arrays.asList(shopId));
//        //创建一个实现类去实现feign接口，加一个fall back，然后引入sentinel依赖
//        if (shopResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
//            throw new BusinessException(shopResult.getMsg());
//        }
//        ShopDetail shopDetail = shopResult.getData().get(0);
//
//        //拿商品
//        Result<List<Sku>> skuResult = orderProdFeign.getSkusByIds(Arrays.asList(skuId));
//        if (skuResult.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
//            throw new BusinessException(skuResult.getMsg());
//        }
//        //取出sku
//        Sku sku = skuResult.getData().get(0);
//
//        //创建店铺集合
//        ArrayList<ShopOrder> shopOrders = new ArrayList<>();
//        //创建店铺对象，new一个就行因为单品只有一个店铺
//
//        List<OrderItem> shopOrderItems = new ArrayList<>();
//        //问题是这里面的shopOrderItems是空的
//        //所以创建具体的OrderItem
//        OrderItem orderItem = new OrderItem();
//        BeanUtils.copyProperties(prodConfirmModel,orderItem);
//        BeanUtils.copyProperties(sku,orderItem);
//
//
//        ShopOrder shopOrder = new ShopOrder(shopId, shopDetail.getShopName(), shopDetail.getShopLogo(), shopDetail.getShopAddress(), shopOrderItems);
//
//        //因为就一个嘛，直接添加
//        shopOrders.add(shopOrder);
//
//
//        orderConfirmVO.setShopOrders(shopOrders);
//
//        //组装算总金额
//        skuBuyModels.add(new SkuBuyModel(skuId,sku.getPrice(),prodCount));
//
//    }
//}
